/**
 * 
 */
package org.statewalker.tokenizer.base;

import org.statewalker.tokenizer.stream.ICharStream;
import org.statewalker.tokenizer.stream.ICharStream.Pointer;

public final class NewLineTokenizer<T> extends AbstractTokenizer<T> {

    /**
     * @param stream
     * @return
     */
    public static String checkNewLine(ICharStream stream) {
        char ch = stream.getChar();
        if (ch == '\r') {
            StringBuffer buf = new StringBuffer();
            buf.append('\r');
            stream.incPos();
            ch = stream.getChar();
            if (ch == '\n') {
                buf.append('\n');
                stream.incPos();
            }
            return buf.toString();
        } else if (ch == '\n') {
            stream.incPos();
            return "\n";
        }
        return null;
    }

    public NewLineTokenizer(ITokenFactory<T> factory) {
        super(factory);
    }

    /**
     * @see org.statewalker.tokenizer.base.Tokenizer.ITokenizer#read(org.statewalker.tokenizer.stream.ICharStream)
     */
    public T read(ICharStream stream) {
        Pointer startPos = stream.getPointer();
        String result = checkNewLine(stream);
        if (result == null)
            return null;
        Pointer endPos = stream.getPointer();
        return newToken(startPos, endPos, result);
    }

}